<?php

class ProjectManager extends PObject { 
	private $_options = array();
	private $_project = array();
	private $_current = 0;
	private $_currentControl = null;
	
	public static $mainWindow = null;
	public static $checkWireFrame = null;
	public static $checkSnapInGrid = null;	
	public static $controlPanel = null;	
	
	private $_icon = array(
		'AppWindow' 		=> array(8, 9),
		'ResizableWindow' 	=> array(8, 9),
		'NakedWindow' 		=> array(8, 9),
		'ToolDialog' 		=> array(8, 9),
		'ModalessDialog' 	=> array(8, 9),
		'ModalDialog' 		=> array(8, 9),
		'PopupWindow' 		=> array(8, 9),
		'Calendar' 		=> array(42, 43),
		'CheckBox' 		=> array(16, 17),
		'ComboBox' 		=> array(32, 33),
		'EditBox' 		=> array(24, 25),
		'Frame' 		=> array(28, 29),
		'Gauge' 		=> array(40, 41),
		'HTMLControl' 	=> array(44, 45),
		'HyperLink' 	=> array(20, 21),
		'ImageButton' 	=> array(12, 13),
		'Label' 		=> array(22, 23),
		'ListBox' 		=> array(30, 31),
		'ListView' 		=> array(48, 49),
		'PushButton' 	=> array(10, 11),
		'Panel' 		=> array(14, 15),
		'RadioButton' 	=> array(18, 19),
		'RTFEditBox' 	=> array(26, 27),
		'ScrollBar' 	=> array(36, 37),
		'Slider' 		=> array(38, 39),
		'Spinner' 		=> array(34, 35),
		'TabControl' 	=> array(46, 47),
		'TreeView' 		=> array(50, 51),
	);
	
	private $_window = array(
		'AppWindow',
		'ResizableWindow',
		'NakedWindow',
		'ToolDialog',
		'ModalessDialog',
		'ModalDialog',
		'PopupWindow'
	);
	
	private $_container = array(
		'Panel',
		'TabControl',
		'Frame'
	);	
	
	public function __construct($options = array()) { 
		$this->_options = $options;	
		
		self::$mainWindow 		= wbobject('MainWindow');
		self::$controlPanel 	= wbobject('ControlPanel');
		self::$checkWireFrame 	= wbobject('checkWireFrame');
		self::$checkSnapInGrid 	= wbobject('checkSnapInGrid');
	}
	
	public function getCurrentKey() { 
		return '__project_'.$this->_current;  
	}
	
	public function createProject() {
		$count = count($this->_project);
		
		$this->_current = $count;
		
		$this->createWindow();
	}
	
	public function updateControl($isTranslate = false) {
		self::$controlPanel->updateControl($this->getUpdateData(), $isTranslate);
	}
	
	public function getUpdateData() {
		$window = $this->getCurrentWindow();
		if ($window) {  	
			return $window->toPropertyList();
		} 
		
		return array();
	}
	
	public function updateProject($data) {
		$window = $this->getCurrentWindow();
		if ($window) {  	
			$window->update($data);
		}
	}
	
	public function refreshProject() {
		$window = $this->getCurrentWindow();
		if ($window) {  	
			$window->refresh(true);
		}
	}
	
	public function createWindow($type = 'AppWindow') {
		$key = $this->getCurrentKey(); 
		
		$window = new FormWindow(array(
			'mainWindow' => self::$mainWindow,
			'parent' => self::$mainWindow,
			'wbtype' => $type,
			'title' => '(Empty Project)',
			'width' => 400,
			'height' => 400,		
			'project' => $key
		));
		
		$window['treeNode'] = $this->createRootNode($window);
		$window['treeNode']->select(true);

		$this->_project[$key] = $window;
		
		$this->updateControl();
	}
	
	public function createControl($type, $options = array()) { 
	
		$tree = wbobject('StructTree');
		
		$node = $tree->selectedNode();
		
		$obj = wbobject($tree->value);
		$wbtype = $obj->getOption('wbtype');
		
		if (in_array($wbtype, $this->_window) || in_array($wbtype, $this->_container)) {  // 상위 객체가 윈도우 일때 		 
			$control = $obj->createControl($type, $options);
			$control['treeNode'] = $this->createChildNode($control, $node);
			 
		} else {  // 일반 컨트롤 일 때, 상위 객체의 최하위로 생성 

			$parentNode = $node->parent;
			$parentNode->select(true);
			
			$parent = wbobject($parentNode->value);
			
			$control = $parent->createControl($type, $options );
					
			$control['treeNode'] = $this->createChildNode($control, $parentNode);
		}
		
	
		if ($control['treeNode']) { 
			$control['treeNode']->select(true);
		}		
		
		// main window update
		$this->updateControl();		
		$this->refreshProject();		

		return $control;
	}
	
	public static function getControlTemplate($type, $options = array()) {
		$class = "form".$type;
		
		return new $class($options);
	} 		
	
	public function getControlList() { 
		$window = $this->getCurrentWindow();
		if ($window) {  	
			$window->getControlList();
		}
	}
	
	public function setCurrentControl($control) {
		$window = $this->getCurrentWindow();
		if ($window) {  	
			$window->setCurrentControl($control);
		} 
	}
	
	/**
	 * 
	 * @return FormControl
	 */
	public function getCurrentControl() {
		$window = $this->getCurrentWindow();
		if ($window) {  
			return $window->getCurrentControl();
		}
	}
	
	/**
	 * 
	 * @return FormWindow
	 */
	public function getCurrentWindow() { 
		return $this->_project[$this->getCurrentKey()];
	}
	

	public function moveToCurrentControl(WBEvent $eo) {
		$window = $this->getCurrentWindow();
		if ($window) {  	
			$window->moveToCurrentControl($eo->x, $eo->y);
		}
	}
	
	public function scaleToCurrentControl(WBEvent $eo) { 
		$window = $this->getCurrentWindow();
		if ($window) {  	
			$window->scaleToCurrentControl($eo->x, $eo->y);
		}
	}	
	
	public function getTreeInfo($control) { 
		$type = $control['wbtype'];
		
		$option = array(
			'caption' => $type,	
			'value' => $control['id'],	
			'image' => $this->_icon[$type][0], 	
			'selectedimage' => $this->_icon[$type][1]
		);			
		
		return $option;
	}
	
	public function createRootNode($control) { 
		return wbobject('StructTree')->createRoot($this->getTreeInfo($control));
	} 
	
	public function createChildNode($control, $parent) { 
		return $parent->createChild($this->getTreeInfo($control));
	}
	
	public function createSiblingNode($control, $sibling) { 
		return $sibling->createSibling($this->getTreeInfo($control));		
	}
	
	public function setFocus($isFocus = false) { 
		$window = $this->getCurrentWindow();
		if ($window) { 
			if ($isFocus) { 
				$window->setFocus();
			}
		}
	}
	
	public function toXML() {

		$root = x_('project');
		
		$description = x_('description');
		$description->add(x_('name')->add(''));
		$description->add(x_('author')->add(''));
		
		$root->add($description);
		
		$windows = x_('windows');
		
		foreach ($this->_project as $win) {
			$windows->add($win->toXML());
		}		

		$root->add($windows);
		
		return $root->toString();	
	}
	
	public function convertProject($xml) { 
		// tree 재구성 
	}
	
	// tree 구성 추가 
	// xml 을 객체생성 
	public function convertControl($xml, $parent) { 
		
	}
	
	public function import($path) { 
		if (is_file($path)) { 
			$xml = simplexml_load_file($path);
			
			$this->convertProject($xml);
		}
	}
	
	public function export($path) { 
		if(is_file($path)) { 
			$data = $this->toXML();
			file_put_contents($path, $data);
		}
	}
}


?>